package jenkins

import (
	"project-info/src/handle/jenkins"
	"project-info/src/router/req"
	"project-info/src/router/variable"

	"github.com/gin-gonic/gin"
)

// init 自动注册Jenkins路由
func init() {
	variable.InitRouteFunc = append(variable.InitRouteFunc, registerJenkinsRoutes)
}

// registerJenkinsRoutes 注册Jenkins相关路由
func registerJenkinsRoutes(e *gin.Engine) {
	jenkinsGroup := req.NewGroup("/jenkins").NeedToken().SetUpGroup(e)
	reqs := []*req.Conf{
		req.NewPost("/getBuildBranch", jenkins.GetBuildBranch),
		req.NewPost("/getBuilds", jenkins.GetJobBuilds),
		req.NewPost("/getRollbacks", jenkins.GetRollbacks),
		req.NewPost("/build", jenkins.JenkinsJobBuild),
		req.NewPost("/rollback", jenkins.JenkinsJobRollback),
		req.NewGet("/wsBuildLog", jenkins.GetJenkinsJobBuildLog),
		req.NewGet("/wsBuildsStatus", jenkins.WsJobBuildStatus),
		req.NewGet("/wsRollbacksStatus", jenkins.WsJobRollbackStatus),
		req.NewGet("/getJobBuildInfo", jenkins.GetJobBuildInfo),
	}
	req.BatchSetUpRouter(jenkinsGroup, reqs)

	// 创建Jenkins凭据管理路由组
	credentialGroup := jenkinsGroup.Group("/credentials")
	reqs = []*req.Conf{
		// 凭据列表查询（支持分页和搜索）
		req.NewPost("/list", jenkins.GetCredentialList),

		// 创建凭据
		req.NewPost("/create", jenkins.CreateCredential),

		// 更新凭据
		req.NewPut("/update", jenkins.UpdateCredential),

		// 删除凭据（单个）
		req.NewDelete("/delete", jenkins.DeleteCredential),

		// 批量删除凭据
		req.NewDelete("/batch-delete", jenkins.BatchDeleteCredentials),

		// 获取凭据详情
		req.NewGet("/detail", jenkins.GetCredentialDetail),

		// 获取凭据选项
		req.NewGet("/options", jenkins.GetCredentialOptions),
	}

	// 批量注册路由
	req.BatchSetUpRouter(credentialGroup, reqs)
}
